﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.JsonEntities;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.FilterSession;
using System.Web.Script.Serialization;

namespace WaiGaoQiaoWeb.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Menu(string moid)
        {
            List<JsonTreeNode> list = this.GetMenuList(moid);
            return Json(list);
        }

        public List<JsonTreeNode> GetMenuList(string moid)
        {
            MenuInfo menu = new MenuInfo();
            IList<MenuInfo> mlist = menu.GetMenuBy("where ModularId = '"+moid.Trim()+"' and PNodeID = 0");
            List<JsonTreeNode> nodes = new List<JsonTreeNode>();
            foreach (var item in mlist)
            {
                JsonTreeNode cnode = new JsonTreeNode();
                cnode.id = item.NodeId.ToString();
                cnode.text = item.Title;
                cnode.value = item.Url;
                cnode.hasChildren = true;
                cnode.classes = "system";
                cnode.isexpand = true;
                cnode.complete = true;
                AddChildNode(cnode,moid);
                nodes.Add(cnode);
            }
            return nodes;
        }

        /// <summary>
        /// 递归添加子节点
        /// </summary>
        /// <param name="node"></param>
        private void AddChildNode(JsonTreeNode node,string moid)
        {
            int pid = Convert.ToInt32(node.id);
            MenuInfo menu = new MenuInfo();
            if (System.Web.HttpContext.Current.Session["user"] == null)
            {
                return;
            }
            string logid = ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName;
            string query = "where ModularId = '" + moid.Trim() + "' and NodeID in (select MenuId from tbt_roleandmenu "
                          + "where RoleId in (select RoleId from Tbt_LogUserAndRole where LogName = '" + logid + "'))";
            IList<MenuInfo> mlist =
                menu.GetMenuBy(query).Where(minfo => minfo.PnodeId == pid).ToList();

            if (mlist != null)
            {
                foreach (var Childitem in mlist)
                {
                    JsonTreeNode nodeChild = new JsonTreeNode();
                    nodeChild.id = Childitem.Sid.ToString();
                    nodeChild.text = Childitem.Title;
                    nodeChild.value = Childitem.Url;
                    nodeChild.hasChildren = false;
                    nodeChild.isexpand = true;
                    node.ChildNodes.Add(nodeChild);
                }
            }
        }

        /// <summary>
        /// 根据登录用户获取模块
        /// </summary>
        /// <returns></returns>
        public ActionResult GetModularByLog()
        {
            string logid = ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName;
            ModularInfo modu = new ModularInfo();
            List<ModularInfo> modulist = modu.GetModularByLog(logid);
            return Json(modulist);
        }
        /// <summary>
        /// 安全退出系统
        /// </summary>
        /// <returns></returns>       
        public ActionResult SafetyExit()
        {
            int result = 0;
            try
            {
                if (System.Web.HttpContext.Current.Session["user"] != null)
                {
                    System.Web.HttpContext.Current.Session["user"] = null;
                }
                result = 1;
                return Json(result);
            }
            catch
            {
                result = 0;
                return Json(result);
            }
        }
    }
}
